//
//  HWOneYuanHttpManager.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/9/1.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import Alamofire
class HWOneYuanHttpManager: NSObject {
    var uid = HWLocallyStoredData.getLoginUserID()
    var tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
    var p = 1
    var max_p = "1"
}

extension HWOneYuanHttpManager {
    /// sign加密
    /// - Returns: sign
    func encrySign() -> String {
        
        let sign = "\(uid)\(tStr)\("fsd213ewdsadqwe2121213edsad")"
        let signMD5 = WMUtil.md5HexDigest(sign) ?? ""
        return signMD5
    }
    func headers() -> HTTPHeaders {
        //        let tStr = Date().timeStamp
        //        let signature = "nnmitu"+tStr
        //
        //        let json = ["usersid":HWLocallyData.getUsersID(),"timestamp":tStr,"signature":signature.DDMD5Encrypt(.lowercase32)]
        //        let jsonString = HWPublicFuncTools.convertDictionAryToJson(dic: json as NSDictionary)
        //        let aesStr:String = aesEncryptString(jsonString, "NrrynIXcpucYvGSk")
        let header:HTTPHeaders = ["eaua":JJUUIDManager.getUUID(),"imei":ASIdentifierManager.shared().advertisingIdentifier.uuidString]
        
        return header
    }
}
extension HWOneYuanHttpManager {
    /// 获取账号交易列表
    func getAccountDetailTradeList(sort:String,system:String,smoney:String,emoney:String,keyword:String,huishouzhanghao:String,success:@escaping (_ models:[HWGameDetailTradeModel]) -> Void, failure:@escaping(_ errorString:String)->Void){
        p = 1
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "sort":sort,
            "system":system,
            "smoney":smoney,
            "emoney":emoney,
            "keyword":keyword,
            "p":p,
            "ios":"1",
            "huishouzhanghao":huishouzhanghao,
            "member_id":UserDefaults.standard.object(forKey: "member_id") ?? ""
        ] as [String : Any]
        
        AF.request(URL(string: URL_AccountTradeList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWGameDetailTradeModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model as! [HWGameDetailTradeModel])
                            
                        }
                        
                    } else if resultDic["code"] as? String == "100" {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
    /// 获取账号交易列表
    func getAccountDetailTradeListFooter(sort:String,system:String,smoney:String,emoney:String,keyword:String,huishouzhanghao:String,success:@escaping (_ models:[HWGameDetailTradeModel]) -> Void, failure:@escaping(_ errorString:String)->Void){
        p += 1
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "sort":sort,
            "system":system,
            "smoney":smoney,
            "emoney":emoney,
            "keyword":keyword,
            "p":p,
            "ios":"1",
            "huishouzhanghao":huishouzhanghao,
            "member_id":UserDefaults.standard.object(forKey: "member_id") ?? ""
        ] as [String : Any]
        
        AF.request(URL(string: URL_AccountTradeList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWGameDetailTradeModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model as! [HWGameDetailTradeModel])
                            
                        }
                        
                    } else if resultDic["code"] as? String == "100" {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                        self.p -= 1
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                        self.p -= 1
                    }
                    
                }else {
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    self.p -= 1
                    
                }
                
            }else {
                failure("获取数据失败")
                print("获取数据失败")
                self.p -= 1
                //                self.p -= 1
            }
        }
    }
   
//    /// 获取账号详情
//    func getTradeDetailInfo(id:String,huishouzhanghao:String,success:@escaping (_ model:HWTradeDetailModel) -> Void, failure:@escaping(_ errorString:String)->Void){
//        p = 1
//        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
//        uid = HWLocallyStoredData.getLoginUserID()
//        let parameters  = [
//            "id":id,
//            "uid":uid,
//            "t":tStr,
//            "sign":encrySign(),
//            "ios":"1",
//            "member_id":UserDefaults.standard.object(forKey: "member_id") ?? ""
//        ] as [String : Any]
//
//        AF.request(URL(string: URL_TradeDetail)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
//            if responses.value != nil {
//
//                let result:String = responses.value ?? ""
//                if result != "" {
//
//                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
//                    if resultDic["code"] as? Int == 1 {
//
//                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
//                        if let model = HWTradeDetailModel.deserialize(from: result ,designatedPath: ".data") {
//                            print(model)
//                            success(model)
//
//                        }else {
//                            failure(resultDic["message"] as? String ?? "")
//                        }
//
//                    } else if resultDic["code"] as? String == "100" {
//                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
//                    }else {
//
//                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
//                        failure(resultDic["message"] as? String ?? "")
//                    }
//
//                }else {
//                    failure("没有获取到数据")
//                    print("没有获取到数据")
//
//                }
//
//            }else {
//                failure("获取数据失败")
//                print("获取数据失败")
//                //                self.p -= 1
//            }
//        }
//    }
}
